package com.vrp3d.common.response;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回前端格式
 * <p>
 * " + CommonConst.NUMBER_OF_BOOTHS + "
 */
@ApiModel
public class RestResponse implements Serializable {

    private static final long serialVersionUID = -421912874895867894L;

    public enum RestResult {
        SUCCESS, ERROR, BAD_REQUEST, WRONG, NOT_FOUND, METHOD_NOT_ALLOWED, DENIED, FORBIDDEN, FILE_SIZE_EXCEEDED,
    }

    /**
     * 头部
     */
    @ApiModelProperty("响应头部")
    private Meta meta;

    /**
     * 数据体
     */
    @ApiModelProperty("响应数据部分")
    private Map<String, Object> data;

    public RestResponse() {
        this("200", "请求成功");
    }

    public RestResponse(String msg) {
        this("200", msg);
    }

    public RestResponse(RestResult restResult) {
        switch (restResult) {
            case SUCCESS:
                this.meta = new Meta("200", "请求成功");
                break;
            case ERROR:
                this.meta = new Meta("500", "系统繁忙，请稍后再试");
                break;
            case BAD_REQUEST:
                this.meta = new Meta("400", "请求参数错误");
                break;
            case DENIED:
                this.meta = new Meta("401", "权限不足");
                break;
            case FORBIDDEN:
                this.meta = new Meta("403", "访问受限");
                break;
            case NOT_FOUND:
                this.meta = new Meta("404", "访问地址不存在");
                break;
            case METHOD_NOT_ALLOWED:
                this.meta = new Meta("405", "请求方式错误");
                break;
            case WRONG:
                this.meta = new Meta("50001", "请求JSON格式错误");
                break;
            case FILE_SIZE_EXCEEDED:
                this.meta = new Meta("50002", "文件大小超限");
                break;
            default:
                break;
        }
        this.data = new HashMap<>();
    }

    public RestResponse(String code, String msg) {
        this.meta = new Meta(code, msg);
        this.data = new HashMap<>();
    }

    public RestResponse put(String key, Object val) {
        this.data.put(key, val);
        return this;
    }

    public RestResponse putException(Object val) {
        this.data.put("exceptionParam", val);
        return this;
    }

    public RestResponse put(Object val) {
        this.data.put("info", val);
        return this;
    }

    public RestResponse setMessage(String message) {
        this.meta.setMessage(message);
        return this;
    }

    public RestResponse appendMessage(String message) {
        String newMessage = this.meta.getMessage() + "," + message;
        this.meta.setMessage(newMessage);
        return this;
    }

    private class Meta {
        @ApiModelProperty("响应头部code码")
        private String code;
        @ApiModelProperty("响应头部信息")
        private String message;

        public Meta(String code, String message) {
            this.code = code;
            this.message = message;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }


}
